<?php





namespace Management\Controller;





use Think\Controller;



class OrderCancelController extends Controller

{



    public function index(){

        $m=M('order');

        $res=$m->where(['status'=>0,'addtime'=>['lt',date('Y-m-d',strtotime('-1 day'))]])->field('id,order_no')->select();

        $model=M('');

        $model->startTrans();

        foreach ($res as $item){

           $where=['order_no'=>$item['order_no']];

           $sku=M('order_detail')->where($where)->field('sku_id,num')->select();

           foreach ($sku as $v){

               $r1=M('sku')->where(['id'=>$v['sku_id']])->setInc('stock',$v['num']);

               if(!$r1) {

                   $model->rollback();

                   $this->ajaxReturn(['rc'=>1]);

               }

           }

           $r2=M('order_detail')->where($where)->delete();

           $r3=$m->delete($item['id']);

           if(!($r2&&$r3)) {

               $model->rollback();

               $this->ajaxReturn(['rc'=>1]);

           }

        }

        $model->commit();

        $this->ajaxReturn(['rc'=>0]);



    }

}